let span_account = document.getElementById("span_account");
let ss_account = sessionStorage.getItem("account");;
span_account.innerText = ss_account;

let books = [
	{id: 1, name: 'Java从入门到放弃', price: 80, tdate: '2020-07-01', count: 1, account: "hehe"},
	{id: 2, name: 'Vue从入门到放弃', price: 90, tdate: '2020-07-02', count: 1, account: "haha"},
	{id: 3, name: 'Mysql从入门到放弃', price: 100.01, tdate: '2020-07-03', count: 1, account: "hehe"},
	{id: 4, name: '啥都从入门到放弃', price: 70.00, tdate: '2020-07-04', count: 1, account: "haha"},
];

let data = localStorage.getItem("data");
if(data) {
	books = JSON.parse(data);
}else {
	localStorage.setItem("data", JSON.stringify(books));
}

let table = document.getElementsByTagName("table")[0];
let tbody = table.getElementsByTagName("tbody")[0];

let ipt_name = document.getElementById("ipt_name");
let ipt_low = document.getElementById("ipt_low");
let ipt_top = document.getElementById("ipt_top");

function search() {
	let name = ipt_name.value;//获取书名
	let low = ipt_low.value;//获取书名
	let top = ipt_top.value;//获取书名
	if(name.trim().length === 0 && low.trim().length === 0 && top.trim().length === 0 ) {
		alert("请输入查询条件")
		return;
	}
	clear();
	update({name, low, top});
	getTotal({name, low, top});
}

function clear() {
	do{
		tbody.removeChild(tbody.firstChild);
	}while(tbody.hasChildNodes());
}

update();
getTotal();
function update(args) {
	for(let index in books) {
		// console.log(index);
		let item = books[index];
		//嵌套地狱：来个if或switch来个18层级嵌套
		// console.log(item.account, ss_account);
		//账号与书本之间的关联处理
		// if(item.account !== ss_account) {
		// 	continue;
		// }
		//搜索功能
		if(args) {
			if(args.name.trim().length !== 0 && item.name !== args.name) {
				continue;
			}
			if(args.low.trim().length !== 0 && item.price < args.low) {
				continue;
			}
			if(args.top.trim().length !== 0 && item.price > args.top) {
				continue;
			}
		}
		// console.log(args);
		
		// console.log(typeof(item));
		let tr = document.createElement("tr");
		tbody.appendChild(tr);
		
		for(let val in item) {
			// console.log(val);
			// console.log(item[val]);
			if(val === "account") {
				continue;
			}
			let text = item[val];
			let isHtml = false;
			if (val === "name") {
				text = `
					<a href='#' onclick='edit(${index})'>${text}</a>
				`;
				isHtml = true;
			}else if(val === 'count') {
				text = `
					<button type="button" onclick="jian(${index})">-</button>
					<span>${text}</span>
					<button type="button" onclick="jia(${index})">+</button>
				`;
				isHtml = true;
			}
			appendTd(tr, text, isHtml);
		}
		let opt = `
			<button onclick='edit(${index})'>编辑</button>
			<button onclick="del(${index})">删除</button>
		`;
		appendTd(tr, opt, true);
	}
}

function appendTd(tr, value, isHtml) {
	let td = document.createElement("td"); 
	if(isHtml) {
		td.innerHTML = value;
	}else {
		td.innerText = value;
	}
	tr.appendChild(td);
}

function getTotal(args) {
	let total = 0;
	
	for(let item of books) {
		if(item.account !== ss_account) {
			continue;
		}
		//搜索功能
		if(args) {
			if(args.name.trim().length !== 0 && item.name !== args.name) {
				continue;
			}
			if(args.low.trim().length !== 0 && item.price < args.low) {
				continue;
			}
			if(args.top.trim().length !== 0 && item.price > args.top) {
				continue;
			}
		}
		total += item.price * item.count;
	}
	
	document.getElementById("span_result").innerText = total;
}


function add() {
	location.href = "add.html";
	sessionStorage.setItem("item", null);
}


function edit(index) {
	let item = books[index];
	sessionStorage.setItem("index", index);
	sessionStorage.setItem("item", JSON.stringify(item));
	location.href = "add.html";
}

function del(index) {
	let result = confirm("确认是否删除" + books[index].name + "这本书？");
	console.log(result);
	if(!result) {
		return;
	}
	//删除数组中的数据
	books.splice(index, 1);
	clear();
	update();
	//更新localStorage
	localStorage.setItem("data", JSON.stringify(books));
	//更新价格
	getTotal();
}